package models

import (
	"fmt"
	"github.com/astaxie/beego/orm"
	"github.com/labstack/gommon/log"
)

/**
通用异常处理类
*/
type Error struct {
	code int
	msg  string
}

func (e *Error) Error() string {
	return fmt.Sprintf("code = %d ; msg = %s", e.code, e.msg)

}

func New(code int, msg string) *Error {
	return &Error{code: code, msg: msg}
}

func CheckError(err error) (resp Response) {
	log.Info(err)
	switch err {
	case orm.ErrNoRows:
		return InitResponse(400, "未查到结果", nil)
	case orm.ErrMissPK:
		return InitResponse(400, "缺少主键", nil)
	default:
		return InitResponse(500, "系统异常", nil)
	}
}
